package com.qf.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        String uri = req.getRequestURI();
        if("/login".equals(uri)) {
            chain.doFilter(request, response);
            return;
        }

        HttpSession session = req.getSession();
        Object obj = session.getAttribute("user");

        if(null == obj) {
            PrintWriter writer = response.getWriter();
            writer.write("please login");
        }else {
            chain.doFilter(request, response);
        }
    }
}
